package com.nikmesoft.android.alwaysontime.daos;


import java.util.ArrayList;
import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import com.nikmesoft.android.alwaysontime.models.Task;


public class TasksDAO extends BaseDAO {	
	private static TasksDAO instance;
	private Context context;
	public int countTasktoday = 0,finishcountTasktoday = 0;

	public TasksDAO(Context context){	
		super(context);
		this.context = context;
	}

	public static void initInstance(Context context){	
		if (instance == null)
		{// Create the instance
			instance = new TasksDAO(context);
		}
	}

	public static TasksDAO getInstance(){	
		// Return the instance
		return instance;
	}

	public ArrayList<Task> getTasks(int TaskType){
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR);
		int minute = c.get(  Calendar.MINUTE);
		int ampm = c.get( Calendar.AM_PM);
		int hour1, minute1, ampm1;
		String dateTaskType;
		String stat;
		
		openDataBase();
		ArrayList<Task> taskList = new ArrayList<Task>();
		String query="";
		switch (TaskType)
		{	case 0 : //today
				dateTaskType = "'"+((day<10)? "0"+day : day)+"/"+((month<10)? "0"+month : month)+"/"+year+"'";
				query = "SELECT * FROM tasks WHERE date = " + dateTaskType + ";";
				break;
			case 1 : //this month
				dateTaskType = "substr(date,7,4) = "+"'"+year+"'"+" and substr(date,4,2) = "+"'"+ ((month<10)? "0"+month : month)+"'";
				query = "SELECT * FROM tasks WHERE " + dateTaskType + ";";
				break;
			case 2 : //all
				query = "SELECT * FROM tasks;";				
				break;
		}		
		
		ArrayList<ArrayList<String>> stringList = selectRecordsFromDBList(query, null);
		close();
		
		if (TaskType==0) {
			countTasktoday = stringList.size();
			finishcountTasktoday = 0;
		} 
		
		for (int i = 0; i < stringList.size(); i++)
		{	ArrayList<String> list = stringList.get(i);
			Task item = new Task();
			try
			{	// set data for item
				item.setId(Integer.parseInt(list.get(0)));
				item.setContent(list.get(1));
				item.setType(list.get(2));
				item.setPriority(Integer.parseInt(list.get(3)));			
				stat =list.get(4);
				if (stat.equals("1")) {item.setStatus(true);
				} else {
					item.setStatus(false);
				}
				//item.setStatus(Boolean.parseBoolean(list.get(4)));
				item.setTime(list.get(6));
				item.setDate(list.get(5));
				item.setSound( list.get(7));
				if (TaskType==0) {
					hour1 = Integer.parseInt(list.get(6).substring(0,2));
					minute1 = Integer.parseInt(list.get(6).substring(3,5));
					if (list.get(6).substring(6,8).equals("AM")) {
						ampm1 = 0;
					} else {
						ampm1 = 1;
					}
					
					if (ampm1 < ampm) {
						finishcountTasktoday +=1;
					} else if (ampm1 == ampm) {
						if (hour1 < hour) {
							finishcountTasktoday +=1;
						} else if (hour1 == hour && minute1 < minute ) {
							finishcountTasktoday +=1;
						} 
					} 
				} 
			}
			catch (Exception e)
			{	Log.i(context.getClass().getSimpleName(), e.getMessage());
			}
			taskList.add(item);
		}
		return taskList;
	}

	public int addTasks(Task item){	
		int newID;
		ContentValues values = new ContentValues();

		// put data from item to values, then write values to database
		values.put("content", item.getContent());
		values.put("type", item.getType());
		values.put("priority", item.getPriority());
		values.put("status", item.getStatus());
		values.put("date", item.getDate());		
		values.put("time", item.getTime());
		values.put("sound", item.getSound());
		
		openDataBase();
		newID = (int) insertRecordsInDB("tasks", null, values);
		close();
		return newID;
	}

	public void deleteTasksByID(int id){	
		openDataBase();
		deleteRecordInDB("tasks", "id=?", new String[] { String.valueOf(id) });
		close();
	}
	public void updateTasks(Task item){
		ContentValues values = new ContentValues();

		// put data from item to values, then write values to database
		values.put("content", item.getContent());
		values.put("type", item.getType());
		values.put("priority", item.getPriority());
		values.put("status", item.getStatus());
		values.put("date", item.getDate());		
		values.put("time", item.getTime());
		values.put("sound", item.getSound());
		
		openDataBase();
		updateRecordInDB("tasks", values, "id=?", new String[] { String.valueOf(item.getId())});
		close();
	}
}
